package servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import model.User;
import service.IUserService;
import service.UserService;
/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        User user=new User();;
        try {
        	UserService service;
        	service=new UserService();

        	String username=request.getParameter("username");
			String password=request.getParameter("password");
			String gender=request.getParameter("gender");
			Integer age=Integer.parseInt(request.getParameter("age"));
			String email=request.getParameter("email");
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
			String regdatetime=formatter.format(cal.getTime()); 
			
			if(service.findUser(username)){
				JOptionPane.showMessageDialog(null, "此用户名已存在", "提示", JOptionPane.INFORMATION_MESSAGE);
				response.sendRedirect("register.jsp");
				
			}
			else{
			user.setUsername(username);
			user.setPassword(password);
			user.setGender(gender);
			user.setAge(age);
			user.setEmail(email);
			user.setRegdatetime(regdatetime);
			       if(service.insertUser(user)){
				     request.getSession().setAttribute("user", user);
				     JOptionPane.showMessageDialog(null, "新用户注册成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
				     response.sendRedirect("login.jsp");
				     //response.sendRedirect("Login.jsp");
			      }
			      else{
				     JOptionPane.showMessageDialog(null, "创建用户失败！", "提示", JOptionPane.INFORMATION_MESSAGE);
				     response.sendRedirect("register.jsp");
				     //response.sendRedirect("Register.jsp");
			      }
		  }
           }catch (Exception e) {
			e.printStackTrace();
		 }
	}
}
